package com.shijiwei.lifthelper.utils;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by sjw on 2015/12/8.
 */
public class FileUtils {

    private static Context context;

    public static void init(Context context) {
        FileUtils.context = context;
    }

    /**
     * 获得文件夹的根路径
     *
     * @return
     */
    public static File getRootPath() {
        if (context == null) {
            Log.e("FileUtils", "请调用init()初始化工具类");
            return null;
        }
        File rootPath = null;
        if (Build.VERSION.SDK_INT >= 14) {
            rootPath = context.getExternalCacheDir();
        } else {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                rootPath = new File(Environment.getExternalStorageDirectory() + File.separator + context.getPackageName());
            } else {
                rootPath = new File(Environment.getDataDirectory() + File.separator + context.getPackageName());
            }
        }

        if (!rootPath.exists()) {
            rootPath.mkdirs();
        }

        return rootPath;
    }

    /**
     * 获得图片文件夹的路径
     *
     * @return
     */
    public static File getPhotoPath() {
        File photoPath = new File(getRootPath() + File.separator + AppConfig.PHOTO_FILE_NAME);
        if (!photoPath.exists()) {
            photoPath.mkdirs();
        }
        return photoPath;
    }

    /**
     * 获得存放图片的文件，以当前（拍照）事件命名
     * @return
     */
    public static File getPhotoFile() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String photoName = format.format(new Date());
        File photoFile = new File(getPhotoPath() + File.separator + photoName + ".png");
        if (!photoFile.exists()){
            try {
                photoFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return photoFile;
    }

    public static File getDbFile(){
        File dbFile = new File(getRootPath() + File.separator + AppConfig.DB_FILE_NAME);
        return null;
    }




}
